using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using ECommerce.Library.DataStructure;
using Entity;

namespace AutoGear.BLL
{
    /// <summary>
    /// This object represents the Private Fields of Video.
    /// </summary>

    public partial class Video
    {
        #region Video Private Fields

        TblVideo _tblVideo;
        Int32 _videoID;
        String _title = String.Empty;
        String _description = String.Empty;
        String _videoPath = String.Empty;
        String _thumbPath = String.Empty;
        DateTime _dateUploaded;
        Boolean? _isDeleted;

        #endregion

        #region Video Constructors
        /// <summary>
        /// Creates an empty object of Video. 
        /// This is mainly used to create a new instance of Video to add a new Video.
        /// </summary>
        public Video()
        {
            _tblVideo = new TblVideo();
        }


        /// <summary>
        /// Used to retrieve employee information from database, 
        /// which is also used by Update method to save information of an existing Video.
        /// </summary>
        public Video
        (

            Int32 videoID,

            String title,

            String description,

            String videoPath,

            String thumbPath,

            DateTime dateUploaded,

            Boolean? isDeleted
        )
        {
            _videoID = videoID;
            _title = title;
            _description = description;
            _videoPath = videoPath;
            _thumbPath = thumbPath;
            _dateUploaded = dateUploaded;
            _isDeleted = isDeleted;
            _tblVideo = new TblVideo(_videoID, _title, _description, _videoPath, _thumbPath, _dateUploaded, _isDeleted);
        }

        #endregion

        #region Video Properties

        /***********************************************************************************************
                                    -- PROPERTIES --        
         Contains all the properties representing the data fields of the current entity.
         ***********************************************************************************************/

        /// <summary>
        /// This is the Primary key value of the VideoID, which can't be assigned from the application,
        /// to ensure the data consistancy, by not letting the user to change the PK_Video to update the data of an another Video instance.
        /// For view or updation purpose, every Video will be retrieved thru the proper 'Get' method.
        /// </summary>

        public Int32 VideoID
        {
            get { return _videoID; }
        }


        public String Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public String Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public String VideoPath
        {
            get { return _videoPath; }
            set { _videoPath = value; }
        }

        public String ThumbPath
        {
            get { return _thumbPath; }
            set { _thumbPath = value; }
        }

        public DateTime DateUploaded
        {
            get { return _dateUploaded; }
            set { _dateUploaded = value; }
        }

        public Boolean? IsDeleted
        {
            get { return _isDeleted; }
            set { _isDeleted = value; }
        }


        #endregion

        #region Video Static Methods

        public static Int32 CreateNewVideo
        (
            String title,
            String description,
            String videoPath,
            String thumbPath,
            DateTime dateUploaded,
            Boolean isDeleted
        )
        {
            //creating an empty Video object
            Video video = new Video();
            video.Title = title;
            video.Description = description;
            video.VideoPath = videoPath;
            video.ThumbPath = thumbPath;
            video.DateUploaded = dateUploaded;
            video.IsDeleted = isDeleted;

            return Video.CreateNewVideo(video);
        }

        public static bool UpdateVideo
        (
            Int32 videoID,
            String title,
            String description,
            String videoPath,
            String thumbPath,
            DateTime dateUploaded,
            Boolean isDeleted
        )
        {
            Video video = Video.GetVideoByVideoID(videoID);
            video.Title = title;
            video.Description = description;
            video.VideoPath = videoPath;
            video.ThumbPath = thumbPath;
            video.DateUploaded = dateUploaded;
            video.IsDeleted = isDeleted;

            return Video.UpdateVideo(video);
        }

        /// <summary>
        /// Deletes a video from the database.
        /// </summary>
        public static bool DeleteVideo(Int32 videoID)
        {
            return TblVideo.DeleteTblVideo(videoID);
        }

        /// <summary>
        /// Deletes a set of video's from the database.
        /// </summary>
        public static bool DeleteVideoList(System.Collections.Generic.List<Int32> videoIDListToDelete)
        {
            return TblVideo.DeleteTblVideoList(videoIDListToDelete);
        }

        public static Video GetVideoByVideoID(Int32 videoID)
        {
            TblVideo tblVideo = TblVideo.GetTblVideoByVideoID(videoID);
            return new Video(tblVideo.VideoID, tblVideo.Title, tblVideo.Description, tblVideo.VideoPath, tblVideo.ThumbPath, tblVideo.DateUploaded, tblVideo.IsDeleted);
        }

        public static ReadWriteCustomCollection<Video> GetAllVideo()
        {
            return LoadFromCustomCollection(TblVideo.GetAllTblVideo());
        }

        public static ReadWriteCustomCollection<Video> GetAllVideoPaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            return LoadFromCustomCollection(TblVideo.GetAllTblVideoPaged(orderBy, startRowIndex, maximumRows));
        }

        public static int GetAllVideoPagedCount()
        {
            return TblVideo.GetAllTblVideoPagedCount("", 0, 0);
        }

        #endregion

        #region Video Instance Methods

        public bool Save()
        {
            _tblVideo.DateUploaded = this.DateUploaded;
            _tblVideo.Description = this.Description;
            _tblVideo.IsDeleted = this.IsDeleted;
            _tblVideo.ThumbPath = this.ThumbPath;
            _tblVideo.Title = this.Title;
            _tblVideo.VideoPath = this.VideoPath;

            if (_tblVideo.Save())
            {
                this._videoID = _tblVideo.VideoID;
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// Creates a new Video instance to the database.
        /// </summary>
        public static Int32 CreateNewVideo(Video newVideo)
        {
            TblVideo newTblVideo = new TblVideo();
            newTblVideo.DateUploaded = newVideo.DateUploaded;
            newTblVideo.Description = newVideo.Description;
            newTblVideo.IsDeleted = newVideo.IsDeleted;
            newTblVideo.ThumbPath = newVideo.ThumbPath;
            newTblVideo.Title = newVideo.Title;
            newTblVideo.VideoPath = newVideo.VideoPath;
            return TblVideo.CreateNewTblVideo(newTblVideo);
        }

        /// <summary>
        /// Updates a Video instance to the database.
        /// </summary>
        public static bool UpdateVideo(Video newVideo)
        {
            TblVideo newTblVideo = new TblVideo();
            newTblVideo.DateUploaded = newVideo.DateUploaded;
            newTblVideo.Description = newVideo.Description;
            newTblVideo.IsDeleted = newVideo.IsDeleted;
            newTblVideo.ThumbPath = newVideo.ThumbPath;
            newTblVideo.Title = newVideo.Title;
            newTblVideo.VideoPath = newVideo.VideoPath;
            return TblVideo.UpdateTblVideo(newTblVideo);
        }
        #endregion

        #region Video Related Collections
        private static ReadWriteCustomCollection<Video> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblVideo> tblVideos)
        {
            ReadWriteCustomCollection<Video> result = new ReadWriteCustomCollection<Video>();
            for (int i = 0; i < tblVideos.Count; i++)
            {
                TblVideo tmpTblVideos = tblVideos[i];
                result.Add(new Video(tmpTblVideos.VideoID, tmpTblVideos.Title, tmpTblVideos.Description, tmpTblVideos.VideoPath, tmpTblVideos.ThumbPath, tmpTblVideos.DateUploaded, tmpTblVideos.IsDeleted));
            }
            return result;
        }

        #endregion

    }
}
